package utils;

public class Ray {

    private Point3D sourcePoint;
    private Vector direction;

    public Ray(Point3D sourcePoint, Vector direction) {
	this.sourcePoint = new Point3D(sourcePoint);
	this.direction = new Vector(direction);
	this.direction = direction.normalize();
    }

    public Ray(Point3D sourcePoint, Point3D destPoint) {
	this(sourcePoint, new Vector(sourcePoint, destPoint));
    }

    public Point3D getSource() {
	return new Point3D(sourcePoint);
    }

    public void setSource(Point3D sourcePoint) {
	this.sourcePoint.deepCopy(sourcePoint);
    }

    public Vector getDirection() {
	return new Vector(direction);
    }

    public void setDirection(Vector direction) {
	this.direction.deepCopy(direction);
    }

    public Point3D getPlaneIntersection(Plane plane) {
	Vector planeNormal = plane.getNormal();
	Vector v = new Vector(sourcePoint, plane.getPoint());
	double numer = v.dotProduct(planeNormal);
	double denom = direction.dotProduct(planeNormal);

	if (numer == 0 || denom == 0 || (numer / denom) < 0) {
	    return null;
	}

	return getPointAtDistance(numer / denom);
    }

    public Point3D getPointAtDistance(double dist) {
	return sourcePoint.addVector(direction.scale(dist));

    }
}
